<template>
  <div class="container">
    <div class="top">
      <el-button type="primary" size="mini" @click="onAdd">新增</el-button>
    </div>
    <div class="content-list">
      <el-table
        ref="table"
        :data="resultList"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
        borderstyle="width: 100%"
      >
        <el-table-column prop="achievementsName" label="成果名称" />
        <el-table-column prop="createTime" label="发布时间" />
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button size="small" type="text" @click="onEdit(row.id)">编辑</el-button>
            <el-button size="small" type="text" @click="onDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pageList">
      <div class="page">
        <paging ref="paging_zi" @CurrentPage="accept_CurrentValue" />
      </div>
    </div>
    <el-dialog :visible="visible" @close="close">
      <div class="add-content">
        <el-form ref="resultFrom" :rules="formRules" :model="resultFrom" label-width="80px">
          <el-form-item prop="achievementsContacts" label="联系人" required>
            <el-input v-model="resultFrom.achievementsContacts" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item prop="achievementsPhoneNumber" label="联系电话" required>
            <el-input v-model="resultFrom.achievementsPhoneNumber" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item prop="achievementsSource" label="成果来源" required>
            <el-input v-model="resultFrom.achievementsSource" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item prop="achievementsName" label="成果名称" required>
            <el-input v-model="resultFrom.achievementsName" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item prop="achievementsContacts" label="成果介绍" required>
            <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 20}"
              v-model="resultFrom.achievementsContent"
              style="width:300px"
            ></el-input>
          </el-form-item>
          <el-form-item prop="region" label="地区" required>
            <el-cascader
              ref="area"
              placeholder="请选择地区"
              v-model="resultFrom.region"
              style="width:200px"
              :options="areaList"
              :props="{ label:'value', value:'value',checkStrictly: true}"
            />
          </el-form-item>
          <el-form-item prop="achievementsImages" label="成果图片" required>
            <template>
              <div class="img-content" v-if="resultFrom.achievementsImages.length">
                <div
                  class="img-box"
                  v-for="(item,index) in resultFrom.achievementsImages"
                  :key="index"
                >
                  <img :src="imgBaseUrl + item" />
                  <div class="mask flex items-middle flex-center">
                    <i
                      @click="onDeleteImg(item,index)"
                      style="color:#fff;"
                      class="el-icon-plus el-icon-delete"
                    ></i>
                  </div>
                </div>
              </div>
              <div class="upload-img flex flex-wrap">
                <el-upload
                  ref="upload"
                  :show-file-list="false"
                  :on-success="uploadSuccess"
                  :before-upload="beforeUpload"
                  :on-error="uploadError"
                  :action="uploadUrl"
                  :data="{ biz: 'pc_web' }"
                >
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
            </template>
          </el-form-item>
        </el-form>
      </div>
      <div class="footer flex flex-end">
        <el-button class="btn-plain" size="small" @click="close">取 消</el-button>
        <el-button type="primary" size="small" @click="onSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { Loading } from "element-ui";
import {
  imgBaseUrl,
  resultAreaInfo,
  achievementsDetail,
  achievementsAdd,
  achievementsUpdate,
  achievementsDelete,
  myScienceAchievements
} from "../../../api/api.js";
import paging from "@/components/paging.vue";
export default {
  components: { paging },
  data() {
    return {
      type: 1, // 1新增 2编辑
      visible: false,
      imgBaseUrl: imgBaseUrl,
      resultList: [],
      areaList: [],
      uploadUrl: "https://bootapi.51bmj.cn/api/home/upload",
      resultFrom: {
        achievementsContacts: "",
        achievementsPhoneNumber: "",
        achievementsSource: "",
        achievementsName: "",
        achievementsContent: "",
        region: [],
        achievementsImages: []
      },
      formRules: {
        achievementsContacts: [
          {
            required: true,
            message: "请输入联系人",
            trigger: "blur"
          }
        ],
        achievementsPhoneNumber: [
          {
            required: true,
            message: "请输入联系电话",
            trigger: "blur"
          },
          { pattern: /^1[0-9]{10}$/, message: "请输入正确的11位手机号码" }
        ],
        achievementsSource: [
          {
            required: true,
            message: "请输入成果来源",
            trigger: "blur"
          }
        ],
        achievementsName: [
          {
            required: true,
            message: "请输入成果名称",
            trigger: "blur"
          }
        ],
        achievementsContent: [
          {
            required: true,
            message: "请输入成果介绍",
            trigger: "blur"
          }
        ],
        province: [
          {
            required: true,
            message: "请选择地区",
            trigger: "change"
          }
        ],
        achievementsImages: [
          {
            required: true,
            message: "请上传图片"
          }
        ]
      },
      form: {
        pageNo: 1,
        pageSize: 10
      }
    };
  },
  mounted() {
    this.getList();
    this.getAreaList();
  },
  methods: {
    // 列表数据
    getList() {
      myScienceAchievements(this.form).then(res => {
        this.resultList = res.result.records;
        this.$refs.paging_zi.totalValue(res.result.total);
      });
    },
    accept_CurrentValue(value) {
      this.form.pageNo = value;
      this.getList();
    },
    // 获取地区
    getAreaList() {
      resultAreaInfo().then(res => {
        this.areaList = res.result;
      });
    },
    onEdit(id) {
      this.type = 2;
      achievementsDetail({ id: id }).then(res => {
        this.resultFrom = res.result;
      });
      this.visible = true;
    },
    onDelete(id) {
      this.$confirm(`确认删除?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        achievementsDelete({ id: id }).then(res => {
          this.getList();
          this.$message({ type: "success", message: "操作成功!" });
        });
      });
    },
    onAdd() {
      this.type = 1;
      this.visible = true;
    },
    onSave() {
      this.$refs["resultFrom"].validate(async (valid, item) => {
        if (valid) {
          if (this.type === 1) {
            achievementsAdd(this.resultFrom).then(res => {
              if (res.code === 200) {
                this.$message({ type: "success", message: "操作成功!" });
                this.getList();
                this.close();
              } else {
                this.$message({ type: "warning", message: res.message });
              }
            });
          } else {
            achievementsUpdate(this.resultFrom).then(res => {
              if (res.code === 200) {
                this.$message({ type: "success", message: "操作成功" });
                this.getList();
                this.close();
              } else {
                this.$message({ type: "warning", message: res.message });
              }
            });
          }
        }
      });
    },
    close() {
      this.visible = false;
      this.resultFrom.achievementsContacts = "";
      this.resultFrom.achievementsPhoneNumber = "";
      this.resultFrom.achievementsSource = "";
      this.resultFrom.achievementsName = "";
      this.resultFrom.achievementsContent = "";
      this.resultFrom.region = [];
      this.resultFrom.achievementsImages = [];
    },
    // 上传图片
    beforeUpload(file) {
      this.loading = Loading.service({
        lock: true,
        fullscreen: true,
        background: "rgba(0, 0, 0, 0)",
        text: "上传中,请稍等...",
        customClass: "dan-loading",
        spinner: "el-icon-loading"
      });
    },
    uploadError() {
      this.loading.close();
      this.$message({
        type: "error",
        message: "服务错误，请联系管理员"
      });
    },
    uploadSuccess(response) {
      this.loading.close();
      if (response.code === 200) {
        let image = response.result;
        this.resultFrom.achievementsImages.push(image);
      } else {
        this.$message({
          type: "error",
          message: response.msg
        });
      }
    },
    onDeleteImg(item, index) {
      this.resultFrom.achievementsImages.splice(index, 1);
    }
  }
};
</script>
<style lang="less" scoped>
.add-content {
  .img-content {
    .img-box {
      position: relative;
      width: 150px;
      height: 150px;
      display: inline-block;
      margin: 0 10px 10px 0;
    }
    img {
      width: 150px;
      height: 150px;
      z-index: 9;
    }
    .mask {
      width: 150px;
      height: 150px;
      background: rgba(0, 0, 0, 0.4);
      position: absolute;
      top: 0;
      font-size: 18px;
      opacity: 0;
      z-index: 99;
    }
    .mask:hover {
      opacity: 1;
    }
  }
  .upload-img {
    border: 1px solid #d9d9d9;
    width: 150px;
    height: 150px;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
}
</style>
